package com.example.redis_springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 操作String类型数据
     */
    @Test
    public void testString(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("city123","beijing");
        Object o = valueOperations.get("city123");
        System.out.println(o);
        redisTemplate.delete("city123");
        valueOperations.set("key1","value1",20l, TimeUnit.SECONDS);

        //如果存在则不操作，否则添加
        Boolean aBoolean = valueOperations.setIfAbsent("city1234", "shanghai");
        System.out.println(aBoolean);
    }


    /**
     * 操作Hash数据
     */
    @Test
    public void testHash(){
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("001","name","xiaoliang");
        hashOperations.put("002","name","xiaoting");
        hashOperations.put("002","age","20");
        hashOperations.put("002","address","shenzhen");
        System.out.println(hashOperations.get("001","name"));

        //获取hash结构中的所有字段
        for (Object key : hashOperations.keys("002")) {
            System.out.println(key);
        }

        //获取hash结构中的所有值
        for (Object value : hashOperations.values("002")) {
            System.out.println(value);
        }
    }


    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush("mylist","123"); //一次添加一个值
        listOperations.leftPushAll("mylist","a","b","c"); //一次可以添加多个值

        //取值
        List mylist = listOperations.range("mylist", 0, -1);
        for (Object o : mylist) {
            System.out.println(o); //倒序
        }

        //获得列表长度
        Long size = listOperations.size("mylist");
        int ISize = size.intValue();
        for (int i=0; i<ISize; i++) {
            ///出队列,正序，先进先出
            Object element = listOperations.rightPop("mylist");
            System.out.println(element);

        }
    }

    @Test
    public void testSet(){
        SetOperations setOperations = redisTemplate.opsForSet();
        //添加成员
        setOperations.add("myset","a","b","c","b");

        //取值
        Set<String> myset = setOperations.members("myset");
        for (String o: myset){
            System.out.println(o);
        }
        System.out.println("-------------");
        //删除成员
        setOperations.remove("myset","b");

        //取值
        Set<String> myset1 = setOperations.members("myset");
        for (String o: myset1){
            System.out.println(o);
        }
    }

    @Test
    public void testZSet(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","a",2.0);
        zSetOperations.add("zset","b",6.0);
        zSetOperations.add("zset","c",4.0);

        Set<String> zset = zSetOperations.range("zset", 0, -1);
        for (String s:zset){
            System.out.println(s);
        }

        System.out.println("--------------");
        //修改分数
        zSetOperations.incrementScore("zset","b",10.0);

        Set<String> zset1 = zSetOperations.range("zset", 0, -1);
        for (String s:zset1){
            System.out.println(s);
        }
        System.out.println("--------------");

        zSetOperations.remove("zset","b");
        Set<String> zset2 = zSetOperations.range("zset", 0, -1);
        for (String s:zset2){
            System.out.println(s);
        }
    }

    /**
     * 通用操作方法
     */
    @Test
    public void testCommmon(){

        //获取redis中所有的key
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }

        //判断某个key是否存在
        Boolean aBoolean = redisTemplate.hasKey("zset");
        System.out.println(aBoolean);
        //删除指定key
        redisTemplate.delete("001");
        //获取可以对应的value数据类型
        DataType myset = redisTemplate.type("myset");
        System.out.println(myset);

    }
}
